package com.icesoft.system.controller;

import com.icesoft.framework.core.controller.BaseCrudRestController;
import com.icesoft.framework.core.exception.BusinessException;
import com.icesoft.framework.core.vo.JsonResult;
import com.icesoft.framework.core.vo.Pagination;
import com.icesoft.system.dto.RoleDTO;
import com.icesoft.system.entity.Permission;
import com.icesoft.system.entity.Role;
import com.icesoft.system.entity.User;
import com.icesoft.system.permission.OperationCons;
import com.icesoft.system.permission.annotation.BindPermission;
import com.icesoft.system.service.impl.PermissionServiceImpl;
import com.icesoft.system.service.impl.RoleServiceImpl;
import com.icesoft.system.util.PingYinUtil;
import com.icesoft.system.util.SecurityUtils;
import com.icesoft.system.vo.RoleVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 角色Controller
 *
 * @author ${author}
 * @date 230326
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@BindPermission(name = "角色管理")
@RequestMapping("/system/role")
public class RoleController extends BaseCrudRestController<Role> {

	private final RoleServiceImpl roleService;

	private final PermissionServiceImpl authorityService;

	/**
	 * 查询ViewObject的分页数据
	 * <p>
	 * url请求参数示例: /list?field=abc&pageSize=20&pageIndex=1&orderBy=id
	 * </p>
	 *
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/query")
	@BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
	public JsonResult getViewObjectListMapping(RoleDTO entity, Pagination pagination) throws Exception {
		return super.getViewObjectList(entity, pagination, RoleVO.class);
	}

	/**
	 * 根据资源id查询ViewObject
	 *
	 * @param id ID
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/{id}")
	@BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
	public JsonResult getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
		return super.getViewObject(id, RoleVO.class);
	}

	/**
	 * 新建
	 *
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/create")
	@BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
	public JsonResult createEntityMapping(@Valid @RequestBody RoleDTO dto) throws Exception {
		if (roleService.isExistByName(dto.getName())) {
			throw new BusinessException("【" + dto.getName() + "】重复");
		}
		User user = SecurityUtils.getCurrentUser();
		dto.setCode("ROLE_" + PingYinUtil.getPinYinHeadChar(dto.getName()));
		dto.setOrgId(user.getOrgId());
		return super.createEntity(dto);
	}

	/**
	 * 更新
	 *
	 * @param dto
	 * @return JsonResult
	 * @throws Exception
	 */
	@PostMapping("/update/{id}")
	@BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
	public JsonResult updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody RoleDTO dto) throws Exception {
		return super.updateEntity(id, dto);
	}

	@PostMapping("/updateMenu/{id}")
	@BindPermission(name = "修改角色菜单", code = OperationCons.CODE_WRITE)
	public JsonResult updateMenu(@PathVariable("id") Long id, @Valid @RequestBody RoleDTO dto) throws Exception {
		return roleService.updateMenu(id, dto);
	}

	@PostMapping("/updateOrg/{id}")
	@BindPermission(name = "修改角色组织机构", code = OperationCons.CODE_WRITE)
	public JsonResult updateOrg(@PathVariable("id") Long id, @Valid @RequestBody RoleDTO dto) throws Exception {
		return roleService.updateOrg(id, dto);
	}

	@PostMapping("/updatePermission/{id}")
	@BindPermission(name = "修改角色权限", code = OperationCons.CODE_WRITE)
	public JsonResult updatePermission(@PathVariable("id") Long id, @Valid @RequestBody RoleDTO dto) throws Exception {
		return roleService.updatePermission(id, dto);
	}

	/**
	 * 根据id删除资源对象
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/delete/{id}")
	@BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
	public JsonResult deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
		return super.deleteEntity(id);
	}

	@Override
	protected void afterCreated(Role entityOrDto) throws Exception {
		super.afterCreated(entityOrDto);
		Permission authority = new Permission();
		authority.setRoleId(entityOrDto.getId());
		authority.setAuthority("/**");
		authorityService.createEntity(authority);
	}

}
